<template>
  <div class="app-container">
    <div class="table-container">
      <div class="left">
        <to-link :val="'YdTHyHh0i'" @to="handleOpen">
          <el-button size="small" type="primary">添加</el-button>
        </to-link>
      </div>
    </div>
    <div class="_list">
      <el-table highlight-current-row ref="tableRow" @selection-change="handleSelectionChange" :data="dataList" style="width: 100%" v-loading.body="listLoading">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="产品名称" width="280">
          <template slot-scope="scope">
            {{scope.row.name}}
          </template>
        </el-table-column>
        <el-table-column label="产品图片" width="480">
          <template slot-scope="scope">
            <div class="thumbnail">
              <img :src="scope.row.thumbnail" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="添加时间">
          <template slot-scope="scope">
            {{scope.row.createdAt | formatDate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <to-link :val="'uAJOy0w9_'" @to="handleUpdata(scope.row)">
              <el-button size="small" type="text">编辑</el-button>
            </to-link>
            <to-link :val="'uAJOy0w9_'" @to="handleDelete(scope.row)">
              <el-button size="small" type="text">删除</el-button>
            </to-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pages">
      <div class="left">
        <to-link :val="'PmEc3qVnn'" @to="handleAll">
          <el-tooltip class='item' effect='dark' content='批量删除' placement='top'>
            <el-button size="small" type="danger">批量删除</el-button>
          </el-tooltip>
        </to-link>
      </div>
      <div class="right" v-if="total > 0">
        <el-pagination @current-change="handleCurrentChange" background :current-page.sync="page" layout="total, prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="产品添加" :visible.sync="dialogVisible" width="450px" top="5vh">
      <el-form ref="dialogForm" :model="form" :rules="dialogFormRules" label-width="90px">
        <el-form-item label="产品名称" prop="describe">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="缩略图" prop="img_path">
          <upload v-model="form.thumbnail" @upload-success="upload_success">
            <img :src="form.thumbnail" v-if="form.thumbnail" class="avatar" />
            <i class="el-icon-plus" v-if="!form.thumbnail"></i>
          </upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import mixinsTable from '@/utils/mixins.table'
import Upload from '@/components/Upload'
export default {
  name: 'categoryList',
  mixins: [mixinsTable],
  data() {
    return {
      servicePath: '/product/goods',
      form: {
        name: '',
        thumbId: '',
        thumbnail: ''
      },
      dialogFormRules: {
        name: [
          { required: true, message: '产品名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    Upload
  },
  created() {
    this.getDada()
  },
  methods: {
    initForm(row) {
      this.setFormVal(row)
    },
    getDada() {
      this.getList()
    },
    FormClear() {
      this.$set(this.form, 'name', '')
      this.$set(this.form, 'thumbId', '')
      this.$set(this.form, 'thumbnail', '')
    },
    setFormVal(row) {
      const formArr = Object.keys(row)
      formArr.map(item => {
        this.$set(this.form, item, this.dialogState === 'update' ? row[item] : '')
      })
    },
    handleCurrentChange(val) {
      this.getList(val)
    },
    upload_success(val) {
      this.$set(this.form, 'thumbId', val.mid)
    }
  }
}

</script>
<style lang="scss">
.thumbnail {
  width: 50px;
  height: 50px;
  float: left;
  img {
    width: 50px;
    height: 50px;
  }
}

</style>
